﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Scheduler.Presentation_Layer
{
    /**Class EmployeeListing
     * @author Sara Tuohy
     * @version 1.0
     * Course : ITEC 4860 Spring 2012
     * Written: March, 2012
     * Displays the list of Employees currently in the DB
     */
    public partial class EmployeeListing : Form
    {
        /**
         * Adapter for accessing DB                              */
        private Business_Logic_Layer.Employee[] EmployeeList;
        /**
         * List for managing Employees                           */
        private ArrayList allEmployees;
        /**
         * UserID of individual logged in                        */
        private string userId;


        /**Method: EmployeeListing
         * Constructor 
         */
        public EmployeeListing()
        {
            InitializeComponent();
            allEmployees = new ArrayList();
            userId = Presentation_Layer.GlobalVariables.GlobalLoggedInUser;
            createList();
            displayList();
        }

        /**Method: createList
         * Retrieves and formats the employee data from the DB
         * Purpose: to format employee data for listing
         */
        private void createList()
        {
            if (!Presentation_Layer.GlobalVariables.GlobalEmployeeListCompiled)
            {
                Business_Logic_Layer.GlobalDataManager globalDataMgr = new Business_Logic_Layer.GlobalDataManager();
                globalDataMgr.CreateEmployeeList();
            }
            EmployeeList = (Business_Logic_Layer.Employee[])GlobalVariables.GlobalEmployeeList;
        }


        /**Method: displayList
         * Adds each formatted line to the list box
         * Purpose: add data to the list box
         */
        private void displayList()
        {
            empListListBox.BeginUpdate();
            foreach (Business_Logic_Layer.Employee employee in EmployeeList)
            {
                empListListBox.Items.Add(formatField(employee));
            }
            empListListBox.EndUpdate();
        }

        /**Method: formatField
         * Pads fields to align data in listbox
         * Purpose: makes the fields line up in an aesthetically pleasing manner
         * @param Business_Logic_Layer.Employee - employee created from DB data
         * @return string formatted for the list box
         */
        private string formatField(Business_Logic_Layer.Employee emp)
        {
            string newRow;
            Business_Logic_Layer.Employee listEmp = emp;
            string listName = listEmp.Name;
            listName = listName.PadRight(40);
            string listEmail = listEmp.Email;
            listEmail = listEmail.PadRight(40);
            newRow = listName + " " + listEmp.Phone + "          " + listEmail + "\n";
            return newRow;
        }

        /**Method: empListReturnbutton_Click
         * Close panel.
         * Purpose: called in showDialog(), this returns to calling panel
         */
        private void empListReturnButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
